import { reqSearchData, reqOrderNav } from '@/api'
import { SEARCH_LIST, ORDER_NAV } from '@/store/mutation-types'

export default {
  namespaced: true,
  state: {
    searchData: [],
    orderNav: []
  },
  actions: {
    async getSearchData({ commit }, params = {}) {
      const res = await reqSearchData(params)
      if (res.code == 200) {
        commit(SEARCH_LIST, res.data)
      } else {
        console.log('Search组件仓库获取searchList失败')
      }
    },
    async getOrderNav({ commit }) {
      const res = await reqOrderNav()
      if (res.code == 200) {
        commit(ORDER_NAV, res.data)
      } else {
        console.log('Search组件仓库获取OrderNav失败')
      }
    },
  },
  mutations: {
    [SEARCH_LIST]: (context, data) => context.searchData = data,
    [ORDER_NAV]: (context, data) => context.orderNav = data
  },
  getters: {
    goodsList(state) {
      if (state.searchData.goodsList) {
        return state.searchData.goodsList
      }
    },
    attrsList(state) {
      if (state.searchData.attrsList) {
        return state.searchData.attrsList
      }
    },
    trademarkList(state) {
      if (state.searchData.trademarkList) {
        return state.searchData.trademarkList
      }
    },
    total(state) {
      if (state.searchData.total) {
        return state.searchData.total
      }
    },
    totalPages(state) {
      if (state.searchData.totalPages) {
        return state.searchData.totalPages
      }
    }
  }
}